<?php

require_once ROOT_SERVICES . DS . 'shipping_provider_interface.php';
require_once ROOT_LIBRARY_HTMLDOM . DS . 'simple_html_dom.php';

// viettel_post
class shipping_provider_1 implements shipping_provider_interface {

    private $_shipment;

    public function __construct($shipment) {
        $this->_shipment = $shipment;
    }

    public function get_status_shipping() {
        try
        {
            $tracking_number = $this->_shipment ['tracking_number'];
            $str_link = $this->_shipment ['url'] !== '' ? $this->_shipment ['url'] : "http://203.106.236.200/official/etracking.php?Submit=Track&capture=";
            $str_link .= $tracking_number;

            $html = file_get_html($str_link);
            $status_list = array();
            $i = 0;

            $table_tr_e = $html->find('.art-PostContent table.content1 tbody tr');
            if (is_array($table_tr_e)) {
                foreach ($table_tr_e as $tr) {
                    if ($i++ >= 2) {
                        $tds = $tr->find('td');
                        $time = html_entity_decode(strip_tags($tds [2]->innertext), ENT_QUOTES, 'UTF-8');
                        $status = html_entity_decode(strip_tags($tds [3]->innertext), ENT_QUOTES, 'UTF-8');

                        preg_match("/([0-9]{2})\/([0-9]{2})\/([0-9]{4}) ([0-9]{2})[\:]([0-9]{2})([\:]([0-9]{2}))*/", $time, $matches);
                        list( $date, $j, $m, $Y, $H, $i ) = $matches;

                        $status_list [] = array(
                            'time' => "{$Y}-{$m}-{$j} {$H}:{$i}:00",
                            'status' => "({$this->_shipment ['shipment_provider_name']}) " . $status
                        );
                    }
                }
            }
            return array('sortable' => TRUE, 'status_list' => $status_list);
        } catch (Exception $e)
        {
            return array('sortable' => FALSE, 'status_list' => null);
        }
    }

}

?>
